package com.dvj.gulimall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMQConfig {


    //@Bean Binding，Queue，Exchange

    /**
     * 容器中的 Binding，Queue，Exchange 都会自动创建（RabbitMQ没有的情况）
     * RabbitMQ 只要有。@Bean声明属性发生变化也不会覆盖
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {

        Map<String, Object> arguments = new HashMap<>();
        /*
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key: order.release.order
         * x-message-ttl: 60000
         */
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000);

        return QueueBuilder.durable("order.delay.queue")
                .withArguments(arguments)
                .build();
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return QueueBuilder.durable("order.release.order.queue").build();
    }

    @Bean
    public Exchange orderEventExchange() {
        return ExchangeBuilder.topicExchange("order-event-exchange")
                .durable(true)
                .build();
    }

    @Bean
    public Binding orderCreateOrderBinding(@Qualifier("orderDelayQueue") Queue queue,
                                           @Qualifier("orderEventExchange") Exchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("order.create.order")
                .noargs();

    }

    @Bean
    public Binding orderReleaseOrderBinding(@Qualifier("orderReleaseOrderQueue") Queue queue,
                                            @Qualifier("orderEventExchange") Exchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("order.release.order")
                .noargs();
    }


    /**
     * 订单释放直接和库存释放进行绑定
     *
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding(@Qualifier("stockReleaseStockQueue") Queue queue,
                                            @Qualifier("orderEventExchange") Exchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("order.release.other.#")
                .noargs();
    }

    @Bean
    public Queue stockReleaseStockQueue() {
        return QueueBuilder.durable("stock.release.stock.queue").build();
    }

    @Bean
    public Queue orderSeckillOrderQueue() {
        return QueueBuilder.durable("order.seckill.order.queue").build();
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding(@Qualifier("orderSeckillOrderQueue") Queue queue,
                                                 @Qualifier("orderEventExchange") Exchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("order.seckill.order")
                .noargs();
    }

}
